<?php

class Application_Model_UserMapper extends Ahs_Model_Mapper
{
    public function __construct(array $options = null)
    {
        parent::__construct($options);
        $this->DbTableName = 'Application_Model_DbTable_Users';
    }
    
    public function fetchById($id)
    {
        $row = $this->getDbTable()->fetchRow(array('user_id = ?' => $id));
        return $this->_toUser($row);
    }
    
    public function fetchIdByEmail($email, $pass)
    {
        
        $row = $this->getDbTable()->fetchRow(array('user_email = ?' => $email, 'user_password = ?' => $pass));
        return $this->_toUser($row);
        
        
    }
    public function fechByEmail($email)
    {
        $row = $this->getDbTable()->fetchRow(array('user_email = ?' => $email,'user_deleted = ?'=>false ));
        
        if($row !== null)
        {
        return $this->_toUser($row);
        }
        
        return false;
    }
    public function fetchByProjectId($projectid)
    {
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $sql = $db->select()->from('users')->join('projects_has_users', 'users_user_id = user_id')->where('projects_project_id = ?',$projectid)->where('user_deleted = ?',false);
        
        $rows = $db->fetchAll($sql);
        return $this->_toUsers($rows);
        
        
    }
    
    public function fetchByEmail($email)
    {
        $row = $this->getDbTable()->fetchRow(array('user_email = ?' => $email));
        
        if($row !== null)
        {
        return $this->_toUser($row);
        }
        
        return false;
    }
    
    public function deleteUserFromProject($userid,$projectid)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $affectedrows=$db->delete('projects_has_users', array('users_user_id = ?' =>$userid, 'projects_project_id = ?'=>$projectid));
        if($affectedrows > 0)
        {
            return true;
        }
        return false;
    }
    
    public function addUser($email, $pass, $givenname, $familyname, $role)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        $count = $db->insert('users', array('user_givenname' => $givenname, 'user_familyname' => $familyname, 'user_email' => $email, 'user_password' => $pass, 'user_role' => $role));
        if($count > 0)
        {
            return true;
        }
        
        return false;
    }
    
//    public function addUserToProject($userid, $projectid)
//    {
//        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
//        
//        $count = $db->insert('projects_has_users', array('users_user_id' => $userid, 'projects_project_id' => $projectid));
//        
//        if($count > 0)
//        {
//            return true;
//        }
//        
//        return false;
//    }
    public function addUserToProject($userid, $projectid)
    {
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        try{
        $count = $db->insert('projects_has_users', array('users_user_id' => $userid, 'projects_project_id' => $projectid));
        }
        catch (Exception $ex)
        {
            return false;
        }
        if($count > 0)
        {
            return true;
        }
        
        return false;
    }
    
//    public function fetchBySearchstring($searchstring)
//    {
//        $rows = $this->getDbTable()->fetchAll();
//        $output = array();
//        
//        if ($rows) {
//            foreach ($rows as $row) { 
//                $haystack = $row['user_email'].$row['user_givenname'].$row['user_familyname'];
//                
//                if(stristr($haystack, $searchstring)){
//                    $output[] = $this->_toUser($row);
//                }
//                
//                
//            }
//        }
//        
//        return $output;
//        
//    }
    public function fetchBySearchstring($searchstring)
    {
        $rows = $this->getDbTable()->fetchAll(array('user_deleted = ?' => false));
        $output = array();
        
        if ($rows) {
            foreach ($rows as $row) { 
                
                /*
                $haystack = $row['user_email']." ".$row['user_givenname']." ".$row['user_familyname'];
                
                if(stristr($haystack, $searchstring)){
                    $output[] = $this->_toUser($row);
                }
                */
                
                $searcharray = array_map('strtolower', array($row['user_email'],$row['user_givenname'],$row['user_familyname'])); // een array met enkel lowercase waardes
                $pieces = explode(" ",$searchstring);
                $piecesfound = 0;
                
                foreach($pieces as $piece)
                {
                    foreach($searcharray as $field)
                    {
                        if(stristr($field, $piece))
                        {
                            $piecesfound++;
                            break;
                        }
                    }
                }
                
                
                //als alles gevonden wordt
                if($piecesfound == count($pieces))
                {
                    $output[] = $this->_toUser($row);
                }
                
            }
        }
        
        return $output;
        
    }
    public function fetchAll()
    {        
        $rows = $this->getDbTable()->fetchAll(array('user_deleted = ?'=> false));

        return $this->_toUsers($rows);
    }
    
    /**
     * @param Zend_Db_Table_Row_Abstract $row
     * @return Application_Model_User 
     */
    private function _toUser($row = null)
    {
        $user = new Application_Model_User();

        if ($row) {
            $user->Id           = $row['user_id'];
            $user->Email        = $row['user_email'];
            $user->Givenname    = $row['user_givenname'];
            $user->Familyname   = $row['user_familyname'];
            $user->Active       = $row['user_active'];
            $user->Role         = $row['user_role'];
            $user->Creationdate = $row['user_creationdate'];
            $user->Deleted      = $row['user_deleted'];
            
        }

        return $user;
    }

    /**
     * @param Zend_Db_Table_Rowset_Abstract $rows
     * @return array
     */
    private function _toUsers($rows = null)
    {
        $users = array();

        if ($rows) {
            foreach ($rows as $row) { 
                $users[] = $this->_toUser($row);
            }
        }
        
        return $users;
    }
    public function delete($userid)
    {
        $data = array('user_deleted' => true);
        $row = $this->getDbTable()->update($data, array('user_id = ?'=>$userid));
        if($row >0)
        {
            return true;
        }
        return false;
    }
    public function suspend($userid)
    {
        $data = array('user_active' => false);
        $row = $this->getDbTable()->update($data, array('user_id = ?'=>$userid));
        if($row > 0)
        {
            return true;
        }
        
        return false;
    }
    
    public function unsuspend($userid)
    {
        $data = array('user_active' => true);
        $row = $this->getDbTable()->update($data, array('user_id = ?' => $userid));
        if($row > 0)
        {
            return true;
        }
        
        return false;
        
    }
    public function save(Application_Model_User $user)
    {
        $data = array(
            'user_email'        => $user->Email,
            'user_givenname'    => $user->Givenname,
            'user_familyname'   => $user->Familyname,
            'user_active'       => $user->Active,
            'user_role'         => $user->Role,
            'user_deleted'      => $user->Deleted,
            
        );

        if (null === $user->Id) {
            $data['user_password'] = $user->Password;
            $data['user_creationdate'] = $user->Creationdate;
            
            $this->getDbTable()->insert($data);
        } else {
            
            $this->getDbTable()->update($data, array('user_id = ?' => $user->Id));
        }
    }

}

